﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ImmovablesManager
{
    /// <summary>
    /// Interaction logic for Log.xaml
    /// </summary>
    public partial class Log : Window
    {
        ObservableCollection<DataGridLogDataView>  dataGridLogDataViews = new ObservableCollection<DataGridLogDataView>();
        public Log()
        {
            InitializeComponent();
            
            RefreshLog();
        }

        public void RefreshLog()
        {
            dataGridLogDataViews = new ObservableCollection<DataGridLogDataView>();

            foreach (var story in AppController.ImmoRepository.GetAllStories())
            {
                if (story.ChangiingTransactionDate != null)
                {
                    string changiingTransactionDateFormat = ((DateTime) story.ChangiingTransactionDate).ToString(System.Globalization.CultureInfo.CreateSpecificCulture("fr-FR"));
                    dataGridLogDataViews.Add(new DataGridLogDataView()
                                                 {
                                                     LogId = story.ChangingStoryId,
                                                     LogCardId = story.ChaginggStoryCardId,
                                                     LogCardNumber = story.ChangingNumberCard,
                                                     LogNewValue = story.ChangingNewValue,
                                                     LogOldValue = story.ChangingOldValue,
                                                     LogOperation = story.ChangingOperation,
                                                     LogWho = GetUserName(story.ChangingUserGuid),
                                                     LogDate = changiingTransactionDateFormat,
                                                     LogState = story.ChangingState 
                                                 });
                }
            }

            DataGridLog.ItemsSource = from item in dataGridLogDataViews orderby item.LogDate select item;
            //dataGridLogDataViews.ToList().OrderBy(x => x.LogDate).ToList();
            

        }
        
        string GetUserName(Guid? userId)
        {
            var user = AppController.ImmoRepository.GetUserById(userId);
            return user.UserFirstName + " " + user.UserLastName + " " + user.UserThirdName;
           // return user.UserFirstName + " " + user.UserLastName + " " + user.UserThirdName;
        }

        private void BtnRefresh_Click(object sender, RoutedEventArgs e)
        {
            RefreshLog();
        }

        private void DataGridLog_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            DataGridLogDataView data = (DataGridLogDataView)e.Row.DataContext;

            if (data.LogState == "edit")
                e.Row.Background = Brushes.LightSkyBlue;
            if (data.LogState == "edit field")
                e.Row.Background = Brushes.LightBlue;
            if (data.LogState == "add")
                e.Row.Background = Brushes.LightGreen;
            if (data.LogState == "delete")
                e.Row.Background = Brushes.LimeGreen;
            if (data.LogState == "view")
                e.Row.Background = Brushes.LightYellow;
        }

    
    }
}
